package com.action
{
	import com.logic.BattleLogic;
	import com.resource.animation.Animation;
	import com.resource.animation.AnimationCacher;
	import com.stage.item.Player;
	import com.utils.time.TimerClock;
	
	import mx.core.Application;

	public class ActionBullet extends BaseAction
	{
		public var bulletId:int;
		private var _anima:Animation;
		private var _speed:Number = 20;
		private var _distance:Number = 500;
		public var skillId:String;
		private var _threadId:int;
		public function ActionBullet(player:Player, skillId:String, bulletId:int, dir:int, x:Number, y:Number)
		{
			super(player);
			this.bulletId = bulletId;
			_dir = dir;
			this.skillId = skillId;
			var data:Object = AnimationCacher.getInstance().getAnimation("res/bullet.dat")
			_anima = new Animation(data);
			Application.application.container.addChild(_anima);
			_anima.x = x;
			_anima.y = y;
			_anima.scaleX = dir;
		}
		
		public override function start(dir:int=1):void{
			_threadId = TimerClock.getInstance().addEnterFrame(enterFrame);
			_anima.gotoAndPlay(0);
		}
		
		protected override function enterFrame():void{
			_anima.enterFrame();
			_anima.x += _dir * _speed;
			_distance -= _speed;
			
			// 碰撞检测
			BattleLogic.bulletHitPlayer(this);
			
			if(_distance <= 0){
				end();
			}
		} 
		
		public override function end():void{
			// 注意：这里有时候会出错，bug
			
			if(Application.application.container.contains(_anima)){
				Application.application.container.removeChild(_anima);
			}
			TimerClock.getInstance().removeEnterFrame(_threadId);
			_threadId = 0;
			delete BattleLogic.bulletList[bulletId];
		}
		
		public function get animation():Animation{
			return _anima;
		}
	}
}